import request from "@/util/request"
import {API_SITE, API_VERSION, USER_MODULE} from "./_prefix";
/**
 * 获取全部用户信息
 */
export const getUserList = () => {
    return request(`${API_SITE}${API_VERSION}${USER_MODULE}/`);
}

/**
 * 创建新用户
 */
export const createUser = ({name, phone, email, expiredAt, password}) => {
    //console.log(`${name},${phone},${email},${expiredAt},${password}`);
    let expiredTime = `${expiredAt.getFullYear()}-`;
    let month = expiredAt.getMonth() + 1;
    let date = expiredAt.getDate();
    expiredTime += (month < 10) ? `0${month}-` : `${month}-`;
    expiredTime += (date < 10 ) ? `0${date}` : `${date}`; 
    return request(`${API_SITE}${API_VERSION}${USER_MODULE}/register`,{
        method: "POST",
        body: {
            name: name,
            phone: phone,
            email: email,
            expireAt: expiredTime,
            password: password
        }
    });
}

/**
 * 更新用户信息
 */
export const updateUser = (userid,{expireAt, password}) => {
    let expiredTime = `${expireAt.getFullYear()}-`;
    let month = expireAt.getMonth() + 1;
    let date = expireAt.getDate();
    expiredTime += (month < 10) ? `0${month}-` : `${month}-`;
    expiredTime += (date < 10 ) ? `0${date}` : `${date}`; 
    return request(`${API_SITE}${API_VERSION}${USER_MODULE}/${userid}/update`,{
        method: "POST",
        body: {
            expireAt: expiredTime,
            newPassword: password
        }
    });
}

/**
 * 删除用户信息
 */
export const deleteUser = (userid) => {
    return request(`${API_SITE}${API_VERSION}${USER_MODULE}/${userid}/delete`,{
        method: "POST",
        body: {}
    })
}